const program = require('commander')
const path = require('path')
const { version } = require('./constants')
import apply from './apply'
//多个指令命令的集合   运行zhu-cli create xx
const mapAction = {
    create: {//创建模板
        alias: 'c', //配置命令的别称
        description: 'create a project', //命令相对应的描述
        examples: [
            'zhu-cli create <project-name>'
        ],
    },
    config: {//配置文件
        alias: 'conf',
        description: 'config project variable',
        examples: [
            'zhu-cli config set <k> <v>',
            'zhu-cli config get <k>',
        ],
    },
    '*': {//根据自己的情况配置别的命令  
        alias: '',
        description: 'command not found',
        examples: [],
    }
}

Object.keys(mapAction).forEach(action => {
    program.command(action) //配置命令的名字
        .alias(mapAction[action].alias) //命令别的名称
        .description(mapAction[action].description) //命令对应的描述
        .action(() => {
            if (action === '*') { //访问不到对应的命令 就打印找不到命令
                console.log(mapAction[action].description)
            } else {
                //运行zhu-cli create xxx  解析后是[node , zhu-cli  , create  , xxx]
                apply(action, ...process.argv.slice(3))
            }
        })
})

program.on('--help', () => {
    Object.keys(mapAction).forEach(example => {
        console.log('  ' + example)
    })
})

program.version(version).parse(process.argv)